package com.ruoyi.order.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.Unpay;
import com.ruoyi.order.domain.VerOrder;
import com.ruoyi.order.domain.dto.VerCartAllDto;
import com.ruoyi.order.mapper.VerOrderMapper;
import com.ruoyi.order.service.IVerOrderService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2025-08-21
 */
@RestController
@Tag(name = "订单接口", description = "提供订单操作")
@RequestMapping("/order/order")
public class VerOrderController extends BaseController {
    @Autowired
    private IVerOrderService verOrderService;
    @Autowired
    private VerOrderMapper verOrderMapper;

    /**
     * 查询订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:list')")
    @Operation(summary = "查询订单列表", description = "传入订单查询对象，返回订单列表")
    @PostMapping("/list")
    public AjaxResult list(VerOrder verOrder) {
        List<VerOrder> list = verOrderService.selectVerOrderList(verOrder);
        return success(list);
    }

    /**
     * 导出订单列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:export')")
    @Operation(summary = "导出订单列表", description = "传入订单查询对象，返回订单列表")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VerOrder verOrder) {
        List<VerOrder> list = verOrderService.selectVerOrderList(verOrder);
        ExcelUtil<VerOrder> util = new ExcelUtil<VerOrder>(VerOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('order:order:query')")
    @Operation(summary = "获取订单详细信息", description = "传入订单 ID，返回订单详细信息")
    @PostMapping(value = "/info/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId) {
        return success(verOrderService.selectVerOrderByOrderId(orderId));
    }

    /**
     * 新增订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:add')")
    @Operation(summary = "新增订单", description = "传入订单对象，返回新增结果")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VerOrder verOrder) {
        return toAjax(verOrderService.insertVerOrder(verOrder));
    }

    /**
     * 修改订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:edit')")
    @Operation(summary = "修改订单", description = "传入订单对象，返回修改结果")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VerOrder verOrder) {
        return toAjax(verOrderService.updateVerOrder(verOrder));
    }

    /**
     * 删除订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @Operation(summary = "删除订单", description = "传入订单 ID，返回删除结果")
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds) {
        return toAjax(verOrderService.deleteVerOrderByOrderIds(orderIds));
    }

    /**
     * 支付
     */
    @PreAuthorize("@ss.hasPermi('order:order:pay')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @Operation(summary = "支付", description = "传入订单对象，返回支付结果")
    @PutMapping("/payment")
    public AjaxResult pay(@RequestBody VerOrder verOrder) {
        return toAjax(verOrderService.pay(verOrder));
    }

    /**
     * 创建待支付订单
     */
    @PreAuthorize("@ss.hasPermi('order:order:insert')")
    @Operation(summary = "添加待支付订单", description = "传入订单信息，创建订单和订单项，返回包含订单ID的订单信息")
    @PostMapping("/addOrderWithItems")
    public AjaxResult addOrder(@RequestBody Unpay unpay) {
        return AjaxResult.success(verOrderService.addOrderWithItems(unpay));
    }

    /**
     * 运输方式
     */
    @PreAuthorize("@ss.hasPermi('order:order:transport')")
    @Operation(summary = "运输方式", description = "传入订单 ID，返回运输方式")
    @PutMapping("/transport")
    public AjaxResult transport(@RequestParam("orderId") Long orderId, @RequestParam Integer shipMethod) {
        return success(verOrderMapper.transport(orderId, shipMethod));
    }

    /**
     * 计算订单项总价
     */

    /**
     * 计算总金额并修改订单状态
     */
    @PreAuthorize("@ss.hasPermi('order:order:total')")
    @Operation(summary = "计算总金额并修改订单状态", description = "传入订单 ID，总金额totalAmount，返回修改结果")
    @PutMapping("/total")
    public AjaxResult total(@RequestParam("orderId") Long orderId,
                            @RequestParam("totalAmount") BigDecimal totalAmount) {
        return success(verOrderMapper.total(orderId, totalAmount));
    }

    /**
     * 根据用户ID获取订单和订单项列表
     */
    @PreAuthorize("@ss.hasPermi('order:order:all')")
    @Operation(summary = "根据用户ID获取订单和订单项列表", description = "传入用户ID，返回订单和订单项列表")
    @PostMapping("/all")
    public AjaxResult getAll(@RequestParam("userId") Long userId) {
        List<VerCartAllDto> list = verOrderService.selectVerOrderListByUserId(userId);
        return success(list);
    }

    /**
     * 获取订单项表原价
     */
    @PreAuthorize("@ss.hasPermi('order:order:getOriginalPrice')")
    @Operation(summary = "获取订单项表原价", description = "传入订单项ID，返回订单项表原价")
    @PostMapping("/getOriginalPrice")
    public AjaxResult getOriginalPrice(@RequestParam("orderItemId") Long id) {
        return success(verOrderService.getOriginalPrice(id));
    }

    /**
     * 修改订单地址id
     */
    @PreAuthorize("@ss.hasPermi('order:order:updateAddressId')")
    @Operation(summary = "修改订单地址id", description = "传入订单ID和地址ID，返回修改结果")
    @PutMapping("/updateAddressId")
    public AjaxResult updateAddressId(@RequestParam("orderId") Long orderId,
                                      @RequestParam("addressId") Long addressId) {
        return success(verOrderMapper.updateAddressId(orderId, addressId));
    }

}
